﻿@model DishOrder
@{
    ViewBag.Title = "OfferEdit";
    Layout = "~/Views/Shared/_LayoutFormDetail.cshtml";
    ViewBag.SaveAction = "SaveOffer";
}
@section Up{
    @Html.HiddenFor(model => model.Id)
    @Html.HiddenFor(model => model.OrderType)
    @Html.HiddenFor(model => model.State)
    @Html.HiddenFor(m => m.SiteId)
    @Html.HiddenFor(m => m.SiteName)
    @Html.nbDateFor(m => m.OrderDate).Required()
    @Html.nbTextBoxFor(model => model.TotalCost).ReadOnly()
}
@section Down{
    @(Html.nbDetailFor<DishOrderItemView>(s =>
    {
        s.ID("DishList");
        s.layoutW(10);
        s.layoutH(130);
        s.Columns(c =>
        {
            c.Bind(e => e.Id).IsKey();
            c.Bind(e => e.DishId).Hide();
            c.BindEnum(e => e.Category, "DishType").Width(30).Title("类别");
            c.BindEnum(e => e.MainMaterialType, "MaterialCategory").Width(50).Title("主料分类");
            c.Bind(e => e.Name).Width(80).Align(GridAlign.Left).Title("菜肴名称");
            c.Bind(e => e.ItemDesc).Width(162).Title("品名信息").Align(GridAlign.Left);
            c.BindControl(e => e.Count).Width(30).Align(GridAlign.Right).Title("客数");
            c.BindControl(e => e.DishCost, GridControlType.Label).Width(30).Align(GridAlign.Right).Title("成本");
            c.BindControl(e => e.TotalCost, GridControlType.TextBox).Align(GridAlign.Right).Width(50).Title("预计总价");
            //c.BindTemplate(60).Template(Url.GridRowClientDeleteLink());
            c.BindTemplate(50)
                .Template(@<a href="javascript:DeleteRow('${Id}')" class="imgbtn2"><span class="Delete">删除</span></a>);
        });
        s.ShowCheckBox(false);
        s.UsePager(false);
        s.Buttons(b =>
        {
            b.BindAddButton("添加菜肴").OnClick("AddDish");
        });
    }))
}
<script type="text/javascript">
    function AddDish() {
        var date = $("#OrderDate").val();
        if (date == "") {
            alert("请您先填写日期，再添加菜肴！");
            return;
        }

        OpenModalDialog('@Url.Action("Lookup", "DishRange")?orderId=@Model.Id&date=' + date,
        {
            width: 850,
            height: 600,
            caption: "从大菜单中添加菜肴",
            onclose: function (dishes) {
                if (dishes) {
                    // 转化数据，将Id更改为新的GUID
                    MapJsonArray(dishes, "Id", "Id");
                    MapJsonArray(dishes, "Cost", "DishCost");
                    for (var i = 0; i < dishes.length; i++) {
                        var DishCost = dishes[i].DishCost;
                        var count = dishes[i].Count;
                        dishes[i].TotalCost = parseFloat(count * DishCost).toFixed(2);
                    }
                    $("#DishList").flexAddRows(dishes);
                    AutoSum();
                }
            }
        });
    }

    $(function () {
        $("#DishList").delegate("#DishList input[name='Count']", 'change', function (e) {
            var $this = $(this);
            var tr = $this.closest("tr");
            var inputDcost = tr.find("input[name='DishCost']");
            var inputTCost = tr.find("input[name='TotalCost']");
            inputTCost.val(parseFloat($this.val() * inputDcost.val()).toFixed(2));
            e.preventDefault();

            AutoSum();
        })
    });

    function AutoSum() {
        var data = $("#DishList").flexGetData();
        var TotalCost = 0;
        for (var i = 0; i < data.length; i++) {
            var Cost = data[i].TotalCost;
            TotalCost += Cost * 1;
        }
        $('#TotalCost').val(parseFloat(TotalCost).toFixed(2));
    }


    function DeleteRow(id) {
        $("#DishList").flexDeleteRowData(id);
        AutoSum();
    }
</script>
